package behaviorpattern.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 中间类
 *
 * @author seer
 * @date 2018/2/7 15:07
 */
public class Subject {
    private List<Observer> observerList = new ArrayList<>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    /**
     * 绑定观察者
     *
     * @param observer
     */
    public void attach(Observer observer) {
        observerList.add(observer);
    }

    /**
     * 通知所有的观察者
     */
    public void notifyAllObservers() {
        observerList.forEach(Observer::update);
    }
}

